/*
 * @Author: your name
 * @Date: 2020-10-19 16:52:46
 * @LastEditTime: 2020-11-02 15:51:54
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /Project/DemoNodeProject/src/Pages/FineFoodRegisterScreen.js
 */
import React, { Component } from 'react'
import { 
    View, 
    Text,
    TextInput,
    Image,
    StyleSheet,
    ScrollView,
    TouchableOpacity
 } from 'react-native'
import { connect } from 'react-redux'
import {
    createOrder,
    orderPayStatus } from '../Actions/OrderActions'
import { Toast } from '@ant-design/react-native'

class CreateOrderInfoScreen extends Component {
    constructor(props) {
        super(props) 
        this.state = {
            BusinessName:'兰州牛肉拉面馆',    //商家名称
            FoodName: '',       //商品名称
            price:0,            //商品价格
            FoodImage:null        //商品图片地址
        };
    }


    shouldComponentUpdate(nextProps, nextState) {
        if (this.props.isFetch && !nextProps.isFetch) {
            if (nextProps.code == 200) {
                Toast.success(nextProps.msg)  
                console.log("生成的订单id:",nextProps.data.orderId); 
            }
        }
        return true
    }
    //下单
    commit(type) {
        if (type === 1) {
            this.props.createOrder({userName:"张三",userPhone:13811421514,orderPrice:12})   
        } else if (type === 2) {
            let {orderId} = this.props.data;
            if (orderId) {
                this.props.orderPayStatus({orderId})
            } else {
                alert('订单不存在')
            }

        }
    }

    onChangeText(type,value) {
        this.setState({[type]:value})
    }
    
    render() {
        let value = API_ROOT + `api/showImage/upload/${this.state.FoodImage}`
        return (
            <View style={{flex:1, backgroundColor:'white'}}>

                <View style={{ flexDirection:'row',justifyContent:'space-between',alignItems:'center',height:50, backgroundColor:'rgb(235,235,235)',marginLeft:10, marginRight:10,marginTop:10,marginBottom:5,padding:5}}>
                    <Text>配送地址</Text>
                    <Text>青海省西宁市湟中县多巴镇上寺村010号</Text>
                    
                    {/* <TextInput value={this.state.BusinessName} onChangeText={this.onChangeText.bind(this,'BusinessName')} placeholder='请输入商家名称'  style={{width:200, height:50,textAlign:'right'}}></TextInput> */}
                </View>

                <View style={{ flexDirection:'row',justifyContent:'space-between',alignItems:'center',height:50, backgroundColor:'rgb(235,235,235)',marginLeft:10, marginRight:10,marginTop:5,marginBottom:0,padding:5}}>
                    <Text>收货人</Text>
                    <Text>张尕卷</Text>

                </View>
                
                <View style={{ flexDirection:'row',justifyContent:'space-between',alignItems:'center',height:50, backgroundColor:'rgb(235,235,235)',marginLeft:10, marginRight:10,marginTop:10,marginBottom:5,padding:5}}>
                    <Text>收货电话</Text>
                    <Text>13811421514</Text>

                </View>

                <TouchableOpacity onPress={this.commit.bind(this,1)} style={{height:44,width:300, justifyContent:'center',alignItems:'center', backgroundColor:'gray',alignSelf:'center',marginBottom:20}}>
                    <Text>下单去支付</Text>
                </TouchableOpacity>


                <TouchableOpacity onPress={this.commit.bind(this,2)} style={{height:44,width:300, justifyContent:'center',alignItems:'center', backgroundColor:'gray',alignSelf:'center'}}>
                    <Text>模拟支付成功</Text>
                </TouchableOpacity>

                
            </View>
        )
    }
}

const mapStateToProps = (state) => {
    let { isFetch, code, data, msg} = state.Order.createOrder || '';
    return {
        isFetch, 
        code, 
        data, 
        msg
    };       
}

const mapDispatchToProps = (dispatch, ownProps) => {
    return {
        createOrder: (paramter)=> dispatch(createOrder(paramter)),
        orderPayStatus: (paramter)=> dispatch(orderPayStatus(paramter)),             
    }
}

export default connect(mapStateToProps,mapDispatchToProps)(CreateOrderInfoScreen)